package com.blog.service.impl;

import com.blog.dao.NodeDao;
import com.blog.pojo.Article;
import com.blog.pojo.Comment;
import com.blog.pojo.NodeKind;
import com.blog.pojo.Page;
import com.blog.service.NodeService;
import com.blog.utils.FindRequestCacheUtil;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.ibatis.type.StringTypeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description:T0D0
 * @Author XieLiKui
 * @Date 2021/10/6
 **/
@Service("nodeService")
public class NodeServiceImpl implements NodeService {

    @Resource(name = "nodeDao")
    private NodeDao nodeDao;

    @Autowired
    private FindRequestCacheUtil cacheUtil;

    //查询文章分类
    @Override
    public ArrayList<NodeKind> findNodeKind() {
        return nodeDao.findNodeKind();
    }

    //根据类型查询文章
    @Override
    public HashMap<String, Object> findBlog(String kindName, String sort, int reqPage) {
        Page page = new Page();
        page.setCount(nodeDao.findBlogCount(kindName,sort));
        int searchIndex = (reqPage - 1) * 35;
        List<Article> articles = nodeDao.findBlog(kindName, sort, searchIndex);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("page",page);
        stringObjectHashMap.put("articles",articles);
        return stringObjectHashMap;
    }


    @Override
    public HashMap<String, Object> findBlogByKeyword(String keywords , String sort , int reqPage) {

        ArrayList<Article> articles = nodeDao.findBlogByKeyword(keywords , sort , (reqPage-1)*35);  //记录文章
        ArrayList<Article> articlesCount = nodeDao.findBlogCountByKeyword(keywords);  //记录条数

        //以10个子查询
        if(articles.size() == 0 && keywords.length() > 10){
            //把关键字以五个字为一组拆分，计算有几组
            int wordCount = keywords.length()%5==0?keywords.length()/10: keywords.length()/10+1;
            //根据组数循环查询
            for(int i = 1 ; i <= wordCount ; i++){

                List<Article> twoSearch;
                twoSearch = nodeDao.findBlogByKeyword(keywords.substring(((i - 1) * 10), i==wordCount?keywords.length():i*10) , sort , (reqPage-1)*35);
                articles = Lists.newArrayList(Iterables.concat(articles,twoSearch));

                List<Article> threeSearch;
                threeSearch = nodeDao.findBlogCountByKeyword(keywords.substring(((i - 1) * 10), i==wordCount?keywords.length():i*10));
                articlesCount = Lists.newArrayList(Iterables.concat(articlesCount,threeSearch));
            }
        }
        //以10个子查询
        if(articles.size() == 0 && keywords.length() > 5){
            //把关键字以五个字为一组拆分，计算有几组
            int wordCount = keywords.length()%5==0?keywords.length()/5: keywords.length()/5+1;
            //根据组数循环查询
            for(int i = 1 ; i <= wordCount ; i++){
                List<Article> twoSearch;
                twoSearch = nodeDao.findBlogByKeyword(keywords.substring(((i - 1) * 5), i==wordCount?keywords.length():i*5) , sort , (reqPage-1)*35);
                articles = Lists.newArrayList(Iterables.concat(articles,twoSearch));

                List<Article> threeSearch;
                threeSearch = nodeDao.findBlogCountByKeyword(keywords.substring(((i - 1) * 5), i==wordCount?keywords.length():i*5));
                articlesCount = Lists.newArrayList(Iterables.concat(articlesCount,threeSearch));
            }
        }
        //集合去重
        for (int j = 0 ; j < articles.size() ; j++) {
            for(int k = j + 1 ; k < articles.size() ; k++){
                if(articles.get(j).getId()==(articles.get(k).getId())){
                    articles.remove(k); //remove(int index)
                    k--;            //一定要记住j--，不然会出错
                }
            }
        }

        //集合去重
        for (int j = 0 ; j < articlesCount.size() ; j++) {
            for(int k = j + 1 ; k < articlesCount.size() ; k++){
                if(articlesCount.get(j).getId()==(articlesCount.get(k).getId())){
                    articlesCount.remove(k); //remove(int index)
                    k--;            //一定要记住j--，不然会出错
                }
            }
        }
        Page page = new Page();
        page.setCount(articlesCount.size());
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("articles",articles);
        stringObjectHashMap.put("page",page);
        return stringObjectHashMap;
    }

    //通过id查文章
    @Override
    public Article findBlogById(int id) {
        return nodeDao.findBlogById(id);
    }

    //通过id查询评论
    @Override
    public List<Comment> findBlogCommentById(int id) {
        return  nodeDao.findBlogCommentById(id);
    }

    //修改文章浏览数
    @Override
    public void updateArticleView(int id) {
        nodeDao.updateArticleView(id);
    }

    //修改文章点赞数
    @Override
    public boolean updateArticleApprove(int id) {
        try {
            nodeDao.updateArticleApprove(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    //修改评论点赞数
    @Override
    public boolean updateCommentApprove(int id) {
        try {
            nodeDao.updateCommentApprove(id);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    //评论文章
    @Override
    public boolean insertArticleComment(int aid, String content) {
        Comment c = new Comment();
        c.setTime(new Date());
        c.setContent(content);
        try {
            nodeDao.insertArticleComment(aid,c);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    //按ID查询文章类型
    @Override
    public List<String> findKindByArticleId(int id) {
        return nodeDao.findKindByArticleId(id);
    }


    //添加文章
    @Override
    public void addArticle(Article article , String[] kinds) {
        //向文章表添加数
        nodeDao.addArticle(article);
        //通过类型名返回kind的ID集合
        List<Integer> kindsId = nodeDao.findKindsIdByKindName(kinds);
        //向中间表添加数据
        nodeDao.addKindMiddle(article.getId(),kindsId);
        //更新kind表
        for (int id : kindsId) {
            int kindCount = nodeDao.findKindCountById(id);
            nodeDao.updateKind(id, kindCount + 1);
        }
    }
}
